using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace collabr8
{
    public partial class Form1 : Form
    {
        private delegate void UpdateStatusCallback(string strMessage);
        private Server mainServer;
        private Keys key1, key2;    // shortcut keys
        private string key_s1;
        char key_s2;
        private Rectangle SelectionRectangle; // portion of screen to be shared
        // if the 'size' parameter is set, it becomes true. If the shortcut is pressed, it returns false
        private bool shareScreen = false;
        Point rectVertex;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            // Parse the server's IP address out of the TextBox
            IPAddress ipAddr = IPAddress.Parse(txtIp.Text);
            // Save the port
            Int32 port = Convert.ToInt32(txtPort.Text);
            // Save the password
            String passwordServer = txtSetPassword.Text;
            // Create a new instance of the ChatServer object
            this.mainServer = new ChatServer(ipAddr, passwordServer, port, SelectionRectangle, shareScreen);
            // Hook the StatusChanged event handler to mainServer_StatusChanged
            ChatServer.StatusChanged += new StatusChangedEventHandler(mainServer_StatusChanged);
            // Start listening for connections
            mainServer.StartListening();
            // Show that we started to listen for connections
            txtLog.AppendText("Monitoring for connections...\r\n");
            
        } 

        public void mainServer_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            // Call the method that updates the form
            this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
        }

        private void UpdateStatus(string strMessage)
        {
            // Updates the log with the message
            txtLog.AppendText(strMessage + "\r\n");
        }

        
        private void btnSize_Click(object sender, EventArgs e)
        {
            shareScreen = true;
            FormSelection fs = new FormSelection();
            fs.ShowDialog();
            Rectangle selectedArea = fs.mRect;
            // http://stackoverflow.com/questions/1438283/find-coordinates-for-point-on-screen
            rectVertex = new Point();
            rectVertex = fs.PointToScreen(new Point(fs.mRect.X, fs.mRect.Y));
            this.SelectionRectangle = new Rectangle(rectVertex.X, rectVertex.Y, fs.mRect.Width, fs.mRect.Height);
            // close and free the form previously hidden
            fs.Dispose();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnShortcut_Click(object sender, EventArgs e)
        {

            Object selectedItem1 = comboBoxKey1.SelectedItem;
            Object selectedItem2 = comboBoxKey2.SelectedItem;
            key_s1 = selectedItem1.ToString();
            key_s2 = (char)selectedItem2.ToString().ToCharArray()[0];

            switch (key_s1)
            {
                case "Ctrl":
                    key1 = Keys.Control;
                    break;
                case "Alt":
                    key1 = Keys.Alt;
                    break;
            }

        }



        private void myKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (((Control.ModifierKeys & Keys.Control) == key1) && e.KeyChar == key_s2)
                MessageBox.Show("Shortcut pressed.");

        }


    }
}